﻿using A.M.ETO;
using A.M.Storage.Application;
using AutoMapper;
using DotNetCore.CAP;
using Q.M.Finance.Application.IServices;
using Q.M.Finance.Domain.DTO;
using Q.M.Finance.Domain.Entity;
using Q.M.Finance.Infrastruecure.IRepository;
using Q.M.Sell.Doamin.DTO;
using Q.M.Sell.Doamin.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Q.M.Finance.Application.Services
{
    public class IncomeServices : BaseServices<income, string>, IIncomeServices
    {
        private readonly IIncomeRepository incomeRepository;
        private readonly IMapper mapper;
        private readonly IClient_payer_informationRepository client_payer_informationRepository;
        private readonly IIncome_typeRepository income_typeRepository;
        private readonly ICapPublisher capPublisher;
        public IncomeServices(IIncomeRepository incomeRepository, IMapper mapper, IClient_payer_informationRepository client_payer_informationRepository, IIncome_typeRepository income_typeRepository, ICapPublisher capPublisher) : base(incomeRepository)
        {
            this.incomeRepository = incomeRepository;
            this.mapper = mapper;
            this.client_payer_informationRepository = client_payer_informationRepository;   
            this.income_typeRepository = income_typeRepository; 
            this.capPublisher = capPublisher;
        }
        /// <summary>
        /// 添加收入管理
        /// </summary>
        /// <param name="incomeAddDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetincomeAddInfo(incomeAddDTO incomeAddDTO)
        {
            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                var income = mapper.Map<income>(incomeAddDTO);
                var res=await incomeRepository.AddInfor_Async(income);
                if (res > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "添加成功";
                    //添加成功需要传一个消息队列  传递哪个模块 还有编号
                    string numberName = "财务系统-" + incomeAddDTO.receiptid;
                    capPublisher.Publish("GetNewNumber", numberName);
                }
                else
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "添加失败";

                }
                return apiReturn;
            }
            catch (Exception)
            {
              
                throw;
            }
        }
        /// <summary>
        /// 修改收入管理
        /// </summary>
        /// <param name="incomeUpdateDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetincomeUpdateInfo(incomeShowDTO incomeUpdateDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var id = incomeRepository.GetShowInfor(x => x.receiptid.Equals(incomeUpdateDTO.receiptid));
            foreach (var item in id)
            {
                item.receiptid = incomeUpdateDTO.receiptid;
                item.income_theme = incomeUpdateDTO.income_theme;
                item.revenuetype_name = incomeUpdateDTO.revenuetype_name;
                item.income_date=incomeUpdateDTO.income_date;
                item.incomes=incomeUpdateDTO.incomes;
            }
            var res = await incomeRepository.UpdInfor_Batch_Async(id);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功";
            }
            return apiReturn;
        }
        /// <summary>
        /// 反填收入管理
        /// </summary>
        /// <param name="receiptid"></param>
        /// <returns></returns>
        public async Task<income> FindincomeInfo(string? receiptid)
        {
            Expression<Func<income, bool>> i = c => c.receiptid == receiptid;
            var sales = incomeRepository.GetShowObj(i);
            return sales;
        }

        /// <summary>
        /// 删除收入管理
        /// </summary>
        /// <param name="receiptid"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Deleteincome(string? receiptid)
        {
            Expression<Func<income, bool>> i = c => c.receiptid == receiptid;
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var income = incomeRepository.GetShowObj(i);
            var list = await incomeRepository.DelInfor_Async(income);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }
        /// <summary>
        /// 收入管理显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageETO<incomeShowDTO> GetincomeInfo(string? receiptid, int pageIndex, int pageSize)
        {
            PageETO<incomeShowDTO> pageETO = new PageETO<incomeShowDTO>();
            Expression<Func<income, bool>> wherexp = i => true;
            if (receiptid != null)
            {
                  wherexp = i => i.receiptid== receiptid;
            }
            var incomelist =  incomeRepository.GetShowInfor(wherexp);
            var payerlist = client_payer_informationRepository.GetShowInfor();
            var incometypelist = income_typeRepository.GetShowInfor();
            var list = (from a in incomelist
                       join b in payerlist on a.customerid equals b.customerid
                       join c in incometypelist on a.revenuetype_name equals c.revenuetype_name
                        select new incomeShowDTO
                       {
                           receiptid = a.receiptid,
                           income_theme = a.income_theme,
                           revenuetype_name = c.revenuetype_name,
                           income_date=a.income_date,
                           incomes=a.incomes,
                           payer=b.payer,
                           payer_bank_account=b.payer_bank_account,
                       }).ToList();
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
    }
}
